Imports System.Data

Partial Class Admin_Products
    Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 2, Secure.ControlTypes.DeleteType)
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 3, Secure.ControlTypes.EditType)
        sec.SetPageAccess(Session("UserName"))
        sec = Nothing

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtCommRec.Attributes.Add("onkeypress", "return checkDecimal(this);")

        If brokers_cmb.Items.Count < 1 Then

            DataGrid.CurrentPageIndex = 0
            Dim zClass As New LookUp.General_Lookup
            brokers_cmb.Items.Clear()
            zClass.Loader(brokers_cmb, "BROKER", "broker_desc", "broker_cd")
            brokers_cmb.Items.Insert(0, "")

            Type_cmb.Items.Clear()
            zClass.Loader(Type_cmb, "PLAN_TYPE", "Desc", "ID")
            Type_cmb.Items.Insert(0, "")

            zClass.LookUpCode = "PRODUCT"

            If Session("provider_cd") <> Nothing Then
                Back_Panel.Visible = True
                zClass.DataGridFill(DataGrid, Results_lbl, Session("provider_cd"))

                brokers_cmb.SelectedItem.Selected = False
                brokers_cmb.Items.FindByValue(Session("broker_cd")).Selected = True
                brokers_cmb.Enabled = False

                providers_cmb.Items.Clear()
                zClass.Loader(providers_cmb, "PROVIDER", "provider_desc", "provider_cd", brokers_cmb.SelectedValue)
                providers_cmb.Items.Insert(0, "")

                providers_cmb.Items.FindByValue(Session("provider_cd")).Selected = True
                providers_cmb.Enabled = False


            Else
                zClass.DataGridFill(DataGrid, Results_lbl)
            End If

        End If

        Dim Edit_Image As Image = CType(Me.Master.FindControl("A3"), Image)
        Edit_Image.ImageUrl = "~/images/redT.PNG"
        Dim Edit_Button As LinkButton = CType(Me.Master.FindControl("Products_btn"), LinkButton)
        Edit_Button.ForeColor = Drawing.Color.Black
        Dim Edit_Panel As Panel = CType(Me.Master.FindControl("Affiliates_Panel"), Panel)
        Edit_Panel.Visible = True
        'Dim Edit_Menu As Button = CType(Me.Master.FindControl("Affiliates_btn"), Button)
        'Edit_Menu.Enabled = False

        SecurePage()
    End Sub

    Protected Sub brokers_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles brokers_cmb.SelectedIndexChanged
        If brokers_cmb.SelectedItem.Text = "" Then Exit Sub

        Dim zClass As New LookUp.General_Lookup
        providers_cmb.Items.Clear()
        zClass.Loader(providers_cmb, "PROVIDER", "provider_desc", "provider_cd", brokers_cmb.SelectedValue)
        providers_cmb.Items.Insert(0, "")
    End Sub

    Protected Sub providers_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles providers_cmb.SelectedIndexChanged
        If providers_cmb.SelectedItem.Text = "" Then Exit Sub
        If AddEdit_Panel.Visible = True Then Exit Sub

        Dim zClass As New LookUp.General_Lookup
        zClass.LookUpCode = "PRODUCT"
        DataGrid.CurrentPageIndex = 0
        zClass.DataGridFill(DataGrid, Results_lbl, providers_cmb.SelectedValue)
    End Sub

    Protected Sub Back_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Back_btn.Click
        Session.Remove("provider_cd")
        Session.Remove("prdt_cd")
        Session.Remove("mode")
        Response.Redirect("~/Admin/Providers.aspx")
    End Sub

    Protected Sub DataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand
        If e.CommandName = "Edit" Or e.CommandName = "View" Then
            Session("mode") = e.CommandName
            Code_txt.Text = Trim(e.Item.Cells(0).Text)
            Session("prdt_cd") = Trim(e.Item.Cells(0).Text)
            Name_txt.Text = Trim(e.Item.Cells(1).Text)
            Term_txt.Text = Trim(e.Item.Cells(3).Text)
            Type_cmb.SelectedItem.Selected = False
            Type_cmb.Items.FindByValue(e.Item.Cells(8).Text).Selected = True
            If e.Item.Cells(4).Text <> "&nbsp;" Then
                txtCommRec.Text = e.Item.Cells(4).Text
            Else
                txtCommRec.Text = ""
            End If


            Dim dm As New DAOMain.CommonDAO
            Dim dt As DataTable = dm.ExecQuery("SELECT A.broker_cd, B.provider_cd" & _
                                                " FROM broker_mstr AS A INNER JOIN" & _
                                                  " provider_mstr AS B ON A.broker_cd = B.broker_cd INNER JOIN" & _
                                                  " prdt_mstr AS C ON B.provider_cd = C.provider_cd " & _
                                                  " WHERE C.prdt_cd = " & dm.Q(Code_txt.Text))
            If dt.Rows.Count > 0 Then
                Me.brokers_cmb.SelectedValue = dt.Rows(0)("broker_cd")
                providers_cmb.Items.Clear()
                Dim zClass As New LookUp.General_Lookup

                zClass.Loader(providers_cmb, "PROVIDER", "provider_desc", "provider_cd", brokers_cmb.SelectedValue)
                providers_cmb.Items.Insert(0, "")
                Me.providers_cmb.SelectedValue = dt.Rows(0)("provider_cd")

                zClass = Nothing
            End If

            OnEditMode() : EnableMost(IIf(e.CommandName = "Edit", True, False))
            brokers_cmb.Enabled = False : providers_cmb.Enabled = False
            If e.CommandName = "View" Then Save_btn.Enabled = False
            Code_txt.Enabled = False

        ElseIf e.CommandName = "Delete" Then
            Dim zClass As New LookUp.General_Lookup
            zClass.LookUpCode = "PRODUCT"

            'If zClass.OKtoDELETE(e.Item.Cells(0).Text) = True Then

            DataGrid.CurrentPageIndex = 0
            zClass.ExecQuery("DELETE FROM prdt_mstr WHERE prdt_cd='" & e.Item.Cells(0).Text & "'")

            If Session("provider_cd") <> Nothing Then
                zClass.DataGridFill(DataGrid, Results_lbl, Session("provider_cd"))
            Else
                zClass.DataGridFill(DataGrid, Results_lbl)
            End If

        End If
    End Sub

    Protected Sub Add_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add_btn.Click
        Session("mode") = "Add" : Code_txt.Text = "" : Name_txt.Text = "" : Term_txt.Text = ""
        OnEditMode() : EnableMost(True)
    End Sub

    Private Sub OnEditMode()
        DataGrid.Enabled = False : Add_btn.Enabled = False : AddEdit_Panel.Visible = True : Code_txt.Focus()
    End Sub

    Private Sub NotOnEditMode()
        DataGrid.Enabled = True : Add_btn.Enabled = True : AddEdit_Panel.Visible = False : Save_btn.Enabled = True
        If Session("provider_cd") = Nothing Then
            brokers_cmb.Enabled = True : providers_cmb.Enabled = True
        End If
        Code_txt.Enabled = True
    End Sub

    Private Sub EnableMost(ByVal e As Boolean)
        Name_txt.Enabled = e : Code_txt.Enabled = e
        Term_txt.Enabled = e : Type_cmb.Enabled = e
        txtCommRec.Enabled = e
    End Sub

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        NotOnEditMode()
        If Session("broker_cd") = Nothing Then
            brokers_cmb.Enabled = True
            providers_cmb.Enabled = True
        End If
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim zClass As New LookUp.General_Lookup
        Dim dao As New DAOMain.PRDT_MSTR_DAO
        Dim vo As New DAOMain.PRDT_MSTR_VO

        With vo
            .PRDT_DESC = Name_txt.Text
            .PRDT_PLAN_TYPE = Trim(Type_cmb.SelectedValue)
            .PRDT_TERM = Val(Term_txt.Text)
            .PRDT_COMM_RECEIVABLE = Val(txtCommRec.Text)
            .PROVIDER_CD = providers_cmb.SelectedValue
        End With

        If Session("mode") = "Edit" Then
            vo.PRDT_LAST_UPD_BY = Session("UserName")
            vo.PRDT_LAST_UPD_DT = Now
            vo.PRDT_CD = Session("prdt_cd")

            dao.Update(vo)

            'zClass.ExecQuery("UPDATE prdt_mstr SET prdt_desc=" & UCase(dao.Q(Name_txt.Text)) & _
            '  ", prdt_plan_type='" & Trim(Type_cmb.SelectedValue) & "'" & _
            '  ", prdt_term=" & Term_txt.Text & _
            '  ", prdt_last_upd_by='" & Session("UserName") & "'" & _
            '  ", prdt_last_upd_dt='" & Now & "'" & _
            '  " WHERE prdt_cd='" & Session("prdt_cd") & "'")

        ElseIf Session("mode") = "Add" Then
            zClass.LookUpCode = "PRODUCT"
            If zClass.DuplicateCode(Code_txt.Text) = False Then
                Dim pf As New PolyFunc.ASPFunc
                pf.MessageAlert(Me, "Similar Product Code exists!")
                pf = Nothing
                Code_txt.Focus()
                Exit Sub

            End If

            With vo
                .PRDT_CREA_BY = Session("UserName")
                .PRDT_CREA_DT = Now
                .PRDT_CD = Code_txt.Text

            End With

            dao.Create_Record(vo)
            'zClass.ExecQuery("INSERT INTO prdt_mstr(prdt_cd,prdt_desc,provider_cd,prdt_term,prdt_plan_type," & _
            '    "prdt_crea_by,prdt_crea_dt) VALUES (" & _
            '    UCase(dao.Q(Code_txt.Text)) & _
            '    "," & UCase(dao.Q(Name_txt.Text)) & _
            '    "," & providers_cmb.SelectedValue.ToString & _
            '    "," & Term_txt.Text & _
            '    ",'" & Type_cmb.SelectedValue & "'" & _
            '    ",'" & Session("UserName") & "'" & _
            '    ",'" & Now & "')")

        End If

        zClass.LookUpCode = "PRODUCT"
        If Session("provider_cd") <> Nothing Then
            zClass.DataGridFill(DataGrid, Results_lbl, Session("provider_cd"))
        ElseIf providers_cmb.SelectedValue <> "" Then
            zClass.DataGridFill(DataGrid, Results_lbl, providers_cmb.SelectedValue)
        Else
            zClass.DataGridFill(DataGrid, Results_lbl)
        End If
        NotOnEditMode()

        vo = Nothing
        dao = Nothing
    End Sub

    Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        DataGrid.CurrentPageIndex = e.NewPageIndex
        Dim zClass As New LookUp.General_Lookup
        zClass.LookUpCode = "PRODUCT"
        If Session("provider_cd") <> Nothing Then
            zClass.DataGridFill(DataGrid, Results_lbl, Session("provider_cd"))
        ElseIf providers_cmb.SelectedValue <> "" Then
            zClass.DataGridFill(DataGrid, Results_lbl, providers_cmb.SelectedValue)
        Else
            zClass.DataGridFill(DataGrid, Results_lbl)
        End If

    End Sub
End Class